<template>
  <div class="com-container">
    <div class="com-chart" ref="stockRef"></div>
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  data() {
    return {
      allData: null,
      myEchart: null,
      //当前展示数据的页数索引
      currentIndex: 0,
      //定时器索引
      timer: null,
      //屏幕适配大小
      titleFontSize: 0,
      //圆形的中心点坐标
      centerArr: [
        ["18%", "40%"],
        ["50%", "40%"],
        ["82%", "40%"],
        ["34%", "75%"],
        ["66%", "75%"],
      ],
      //圆环的颜色
      colorArr: [
        ["#4FF778", "#0BA82C"],
        ["#E5DD45", "#E8B11C"],
        ["#E8821C", "#E55445"],
        ["#5052EE", "#AB6EE5"],
        ["#23E5E5", "#2E72BF"],
      ],
    };
  },
  created() {
    this.$socket.registerCallBack("stockData", this.getData);
  },
  mounted() {
    this.initEchart();
    // this.getData();
    this.$socket.send({
      action: "getData",
      socketType: "stockData",
      chartName: "stock",
      value: "",
    });
    this.screenAdapter();
    window.addEventListener("resize", this.screenAdapter);
  },
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter);
    clearInterval(this.timer);
    this.$socket.unRegisterCallBack("stockData");
  },
  computed: {
    ...mapState(["theme"]),
  },
  watch: {
    theme() {
      //销毁当前图表
      this.myEchart.dispose();
      //以最新的主题初始化图表对象
      this.initEchart();
      //屏幕适配
      this.screenAdapter();
      //渲染数据
      this.updateChart();
    },
  },
  methods: {
    initEchart() {
      this.myEchart = this.$echarts.init(this.$refs.stockRef, this.theme);
      const initOption = {
        title: {
          text: "▎库存和销量分析",
          top: "5%",
          left: "3%",
        },
      };
      this.myEchart.setOption(initOption);
      this.myEchart.on("mouseover", () => {
        clearInterval(this.timer);
      });
      this.myEchart.on("mouseout", () => {
        this.startInterval();
      });
    },
    getData(res) {
      //   const { data: res } = await this.$http.get("stock");
      // console.log(res);
      this.allData = res;
      this.updateChart();
    },
    updateChart() {
      //处理准备数据
      const start = this.currentIndex * 5;
      const showData = this.allData.slice(start, start + 5);
      const seriesArr = showData.map((item, index) => {
        return {
          type: "pie",
          //外圆半径和内圆半径
          radius: [this.titleFontSize * 2.3, this.titleFontSize * 1.8],
          center: this.centerArr[index],
          //鼠标经过动画效果移除
          hoverAnimation: false,
          labelLine: {
            //隐藏指示线
            show: false,
          },
          label: {
            position: "center",
            color: this.colorArr[index][0],
            fontSize: this.titleFontSize / 2,
          },
          data: [
            //销量
            {
              value: item.sales,
              itemStyle: {
                color: new this.$echarts.graphic.LinearGradient(0, 1, 0, 0, [
                  {
                    offset: 0,
                    color: this.colorArr[index][0],
                  },
                  {
                    offset: 1,
                    color: this.colorArr[index][1],
                  },
                ]),
              },
            },
            //库存
            {
              name: item.name + "\n" + item.sales,
              value: item.stock,
              itemStyle: {
                color: "#333843",
              },
            },
          ],
        };
      });
      const dataOption = {
        series: seriesArr,
      };
      this.myEchart.setOption(dataOption);
    },
    screenAdapter() {
      this.titleFontSize = (this.$refs.stockRef.offsetWidth / 100) * 4.2;
      const data = {
        type: "pie",
        radius: [this.titleFontSize * 2.3, this.titleFontSize * 1.8],
        label: {
          fontSize: this.titleFontSize / 2,
        },
      };
      const tempSeries = [];
      for (let i = 0; i < 5; i++) tempSeries.push(data);
      const adaptOption = {
        title: {
          textStyle: {
            fontSize: this.titleFontSize,
          },
        },
        series: tempSeries,
      };
      this.myEchart.setOption(adaptOption);
      this.myEchart.resize();
    },
    startInterval() {
      clearInterval(this.timer);
      this.timer = setInterval(() => {
        this.currentIndex++;
        if (this.currentIndex >= Math.ceil(this.allData.length / 5))
          this.currentIndex = 0;
        this.updateChart();
      }, 5000);
    },
  },
};
</script>